package georah.carto
{

    import flash.geom.Point;

    import georah.carto.geometries.OGCPoint;

    /**
     *
     * @author yuanm
     */
    public final class SRS
    {
        public static function fromPixelToWhere(pixel:Point, view:IGISClient):OGCPoint
        {
            var x:Number;
            var y:Number;
            var bbox:Extent = view.envelope;

            switch (view.camera.where.SRID)
            {
                case 4326:
                    x = pixel.x / view.width * bbox.width + bbox.minX;
                    y = bbox.maxY - pixel.y / view.height * bbox.height;
                    break;
                default:
                    throw ReferenceError("SRID " + view.camera.where.SRID + " Not Supported");
                    break;
            }
            return new OGCPoint(view.camera.where.SRID, x, y);
        }

        public static function fromWhereToPixel(where:OGCPoint, view:IGISClient):Point
        {
            var x:Number;
            var y:Number;
            var bbox:Extent = view.envelope;

            switch (view.camera.where.SRID)
            {
                case 4326:
                    x = view.width / bbox.width * (where.x - bbox.minX);
                    y = view.height / bbox.height * (bbox.maxY - where.y);
                    break;
                default:
                    throw ReferenceError("SRID " + view.camera.where.SRID + " Not Supported");
                    break;
            }

            return new Point(x, y);
        }

        public static function project(where:OGCPoint, srid:int):OGCPoint
        {
            var x:Number;
            var y:Number;
            switch (where.SRID)
            {
                case 4326:
                    switch (srid)
                {
                    case 900913:
                        x = where.x;
                        y = Math.log(Math.tan(0.25 * Math.PI + 0.5 * where.y));
                        break;
                }
                    break;

                case 900913:
                    switch (srid)
                {
                    case 4326:
                        x = where.x;
                        y = 2 * Math.atan(Math.pow(Math.E, where.y)) - 0.5 * Math.PI;
                        break;
                }
                    break;
            }

            if (x is undefined || y is undefined)
            {
                throw ReferenceError("Project from SRID " + where.SRID + " to SRID " + srid + " Not Supported");
            }
            return new OGCPoint(srid, x, y);
        }

    }
}
